using System;
using System.Web.Mvc;
using Guestbook.Domain;

namespace Guestbook.Controllers
{
    public class GuestbookController : Controller
    {
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Index()
        {
            return View("Index", Domain.Guestbook.Instance);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult New()
        {
            return View("New");
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Add(string name, string comment)
        {
            if (string.IsNullOrEmpty(name))
            {
                ViewData["error"] = "name_empty";
                ViewData["comment"] = comment;

                return View("New");
            }
            else if (string.IsNullOrEmpty(comment))
            {
                ViewData["error"] = "comment_empty";
                ViewData["name"] = name;

                return View("New");
            }
            else
            {
                var entry = new Entry();
                entry.Name = name;
                entry.Comment = comment;
                entry.Date = DateTime.Now;

                Domain.Guestbook.Instance.Add(entry);

                return RedirectToAction("Index");
            }
        }
    }
}
